package com.teachoa.bean;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.teachoa.constant.Global;
import com.teachoa.enumtype.AskStatusEnum;

@SuppressWarnings({"rawtypes", "unchecked"})
public class Ask extends Model<Ask> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7908110352644780962L;
	
	public static final Ask dao = new Ask();
	
	public Page<Ask> findAsksByPage(int pageNo, int userType, String userId) {
		String suffix = "from ask a ";
		List params = new ArrayList();
		switch(userType) {
		case 2:
			suffix+="where a.student_id = ?";
			params.add(userId);
			break;
		case 1:
		case 3:
			suffix += "left join ask_reply b on a.id = b.ask_id where b.replier_id = ?";
			params.add(userId);
			break;
		}
		suffix+=" order by a.create_time desc";
		Page<Ask> askPage = dao.paginate(pageNo, Global.pageSize, "select a.*", suffix, params.toArray());
		for(Ask ask:askPage.getList()) {
			ask.put("status_display", AskStatusEnum.getEnumByCode(ask.getInt("status")).getDesc());
		}
		return askPage;
	}
	
}
